<?php

namespace App\Containers\AppSection\Data\Actions;

use App\Containers\AppSection\Data\Models\Passport;
use App\Containers\AppSection\Data\Tasks\UpdatePassportTask;
use App\Containers\AppSection\Data\UI\API\Requests\UpdatePassportRequest;
use App\Containers\AppSection\Log\Tasks\CreateLogTask;
use App\Containers\AppSection\Profile\Tasks\UpdateProfileTask;
use App\Ship\Parents\Actions\Action;

class UpdatePassportAction extends Action
{
    public function run(UpdatePassportRequest $request): Passport
    {
        $data = $request->sanitizeInput([
            'serial_number',
            'issue_date',
            'department_code',
            'issued_by',
            'first_photo_id',
            'second_photo_id',
        ]);

        //Обновление персональных данных
        $passport = app(UpdatePassportTask::class)->run($request->profile()->legal_form->passport->id, $data);

        //Смена статуса аккредитации
        if (!$request->profile()->hasAccreditationStatus('accreditation_returned'))
            app(UpdateProfileTask::class)
                ->run($request->profile()->id, [
                    'accreditation_status_key' => 'accreditation_not_accredited',
                    'accredited_at' => null
                ]);


        app(CreateLogTask::class)->run($request->profile()->id, 'Редактирование паспортных данных', 'profile');

        return $passport;
    }
}
